package ca.swank.hardquor;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* Constructs Java/Swing components with preferred default values. This class
* is a toolkit with only static methods, so instances of this class should not be
* created. Use the methods of this class in place of Swing constructors to ensure
* default behaviours are consistent.
*
* @author Jesse Wilson
* @version 1.0 9/23/2001
*/
public class hardquorSwingMaker
{

  /**
  * creates a <CODE>GridBagConstraints</CODE> object for inserting components into
  * a <CODE>GridBagLayout</CODE>.
  *
  * @param x the column of the grid that the corresponding object lives in.
  * @param y the row of the grid that the corresponding object lives in.
  * @param align a string describing how to align the corresponding object
  *        in its' cell. Accepted values are <CODE>"N"</CODE>, <CODE>"NW"</CODE>...
  *        for North, Northwest etc., <CODE>"H"</CODE> for stretch horizontal,
  *        <CODE>"V"</CODE> for stretch vertical and <CODE>"B"</CODE> for stretch
  *        in both directions.
  * @return a constraints object for using with add(component, constraints) to
  *        a <CODE>GridBagLayout</CODE>.
  */
  public static GridBagConstraints cell(int x, int y, String align)
  { return cell(x, y, align, 1, 1);
  }

  /**
  * creates a <CODE>GridBagConstraints</CODE> object for inserting components that
  * span multiple rows and/or columns into a <CODE>GridBagLayout</CODE>.
  *
  * @param x the leftmost column that the corresponding component lives in.
  * @param y the topmost row that the corresponding component lives in.
  * @param align a string describing how to align the corresponding object
  *        in its' cell. Accepted values are <CODE>"N"</CODE>, <CODE>"NW"</CODE>...
  *        for North, Northwest etc., <CODE>"H"</CODE> for stretch horizontal,
  *        <CODE>"V"</CODE> for stretch vertical and <CODE>"B"</CODE> for stretch
  *        in both directions.
  * @param width the number of columns for the corresponding object to span.
  * @param height the number of rows for the corresponding object to span.
  * @return a constraints object for using with add(component, constraints) to
  *        a <CODE>GridBagLayout</CODE>.
  */
  public static GridBagConstraints cell(int x, int y, String align, int width, int height)
  { GridBagConstraints cell = new GridBagConstraints();
    cell.gridx = x;
    cell.gridy = y;
    cell.gridwidth = width;
    cell.gridheight = height;
    if(align.equals("C")) cell.anchor = GridBagConstraints.CENTER;
    else if(align.equals("N")) cell.anchor = GridBagConstraints.NORTH;
    else if(align.equals("NE")) cell.anchor = GridBagConstraints.NORTHEAST;
    else if(align.equals("E")) cell.anchor = GridBagConstraints.EAST;
    else if(align.equals("SE")) cell.anchor = GridBagConstraints.SOUTHEAST;
    else if(align.equals("S")) cell.anchor = GridBagConstraints.SOUTH;
    else if(align.equals("SW")) cell.anchor = GridBagConstraints.SOUTHWEST;
    else if(align.equals("W")) cell.anchor = GridBagConstraints.WEST;
    else if(align.equals("NW")) cell.anchor = GridBagConstraints.NORTHWEST;
    else if(align.equals("H")) cell.anchor = GridBagConstraints.HORIZONTAL;
    else if(align.equals("V")) cell.anchor = GridBagConstraints.VERTICAL;
    else if(align.equals("B")) cell.fill = GridBagConstraints.BOTH;

    return cell;
  }

  /**
  * creates a <CODE>JButton</CODE> and registers it with the specified
  * <CODE>ActionListener</CODE>.
  *
  * @param label the text label for the button.
  * @param listener an <CODE>ActionListener</CODE> to handle actions of the button.
  * @return a button with the specified label, registered with the appropriate
  *        <CODE>ActionListener</CODE>.
  */
  public static JButton button(String label, ActionListener listener)
  { JButton button = new JButton(label);
    button.addActionListener(listener);
    return button;
  }

  /**
  * creates a <CODE>JTextField</CODE> with the initial text specified of size
  * specified
  *
  * @param text the initial text value of the text field.
  * @param columns the width of the text field.
  * @return a text field with the specified initial text of size specified.
  */
  public static JTextField textField(String text, int columns)
  { return new JTextField(text, columns);
  }

  /**
  * creates a <CODE>JTextField</CODE> that is registered with the specified
  * <CODE>ActionListener</CODE>
  *
  * @param text the initial text value of the text field.
  * @param columns the width of the text field.
  * @param listener the <CODE>ActionListener</CODE> to notify when this text
  *        field is activated. Text Fields are activated when the user hits
  *        <CODE>ENTER</CODE> when focused in the field.
  * @return a text field that is registered with the specified <CODE>ActionListener</CODE>.
  */
  public static JTextField textField(String text, int columns, ActionListener listener)
  { JTextField textField = new JTextField(text, columns);
    textField.addActionListener(listener);
    return textField;
  }

  /**
  * creates a <CODE>JTextArea</CODE> of size specified.
  *
  * @param columns the number of columns (width) of the text area.
  * @param rows the number of rows (height) of the text area.
  * @return a text field of the specified size.
  */
  public static JTextArea textArea(int columns, int rows)
  { return new JTextArea(rows, columns);
  }

  /**
  * creates a <CODE>JScrollPane</CODE> to scroll the specified component.
  *
  * @param view the <CODE>Component</CODE> to be wrapped by this scroll pane.
  * @return a scroll pane that wraps the specified component.
  */
  public static JScrollPane scrollPane(Component view)
  { return new JScrollPane(view);
  }

  /**
  * creates a <CODE>JLabel</CODE> of the specified text.
  *
  * @param text the message of the label.
  * @return a label of the specified text.
  */
  public static JLabel label(String text)
  { return new JLabel(text);
  }

  /**
  * creates a <CODE>JComboBox</CODE> for selecting from a set of options
  *
  * @param editable true if the user can edit options
  * @return a combobox which is editable if specified
  */
  public static JComboBox comboBox(boolean editable)
  { JComboBox comboBox = new JComboBox();
    comboBox.setEditable(editable);
    return comboBox;
  }


}